
use closed1990-2004.dta
keep if year == 1990
rename reporter country
rename product hs6 
keep country hs6 year closed R
sort country hs6
save closed_1990, replace

use endowments_intensities_1990, clear
sort country hs6
merge country hs6 using closed_1990
gen HK_dist = (HK_country - HK_prod)^2
gen  K_dist =  (K_country -  K_prod)^2
gen  T_dist =  (T_country -  T_prod)^2
gen dist = (HK_dist + K_dist + T_dist)^0.5 if closed == 1
drop HK_dist K_dist T_dist _merge
keep if closed == 1 & dist != .
save closed_data, replace

foreach j of numlist 1991/2003 {
	use closed1990-2004
	keep if year == `j'
	rename reporter country
	rename product hs6 
	keep country hs6 year closed R
	sort country hs6
	save closed_`j', replace
	
	use endowments_intensities_`j', clear
	sort country hs6
	merge country hs6 using closed_`j'
	save closed_`j'_data, replace
	gen HK_dist = (HK_country - HK_prod)^2
	gen  K_dist =  (K_country -  K_prod)^2
	gen  T_dist =  (T_country -  T_prod)^2
	gen dist = (HK_dist + K_dist + T_dist)^0.5 if closed == 1
	drop HK_dist K_dist T_dist _merge
	keep if closed == 1 & dist != .
	append using closed_data
	save closed_data, replace
}

use closed_data, clear

replace R = 0 if R == .

* figure 7a

twoway (kdensity dist if R==1, lcolor(red) lpattern(longdash))(kdensity dist if R!=1, lcolor(blue)), ytitle(Kernel Density) xtitle(Distance) legend(off)




